<?php

/**
 * Display a list of feedbacks for a parent/block.
 *
 * @package    mahara
 * @subpackage artefact-feedback
 * @author     laurent.opprecht@gmail.com
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2011 University of Geneva http://www.unige.ch/
 *
 */
define('INTERNAL', 1);
define('MENUITEM', 'myportfolio');

require(dirname(dirname(dirname(__FILE__))) . '/init.php');
require_once(get_config('libroot') . 'view.php');
safe_require('artefact', 'feedback');

require_once(get_config('docroot') . 'blocktype/lib.php');

$parent_id = param_integer('parent_id');
$view_id = param_integer('view_id');
$show_form = param_boolean('show_form', false);
$goto = get_config('wwwroot') . '/view/view.php?id=' . $view_id;

$view = new view($view_id);
$can_edit = ArtefactTypeFeedback::can_edit($parent);

$show_private = ArtefactTypeFeedback::is_view_owner($view_id);
$count = ArtefactTypeFeedback::count_feedbacks_onparent($parent_id, $show_private);
$limit = param_integer('limit', 10);
$offset = param_integer('offset', 0);
$data = ArtefactTypeFeedback::get_feedbacks_onparent($parent_id, $show_private, $limit, $offset);
$table_id = FeedbackTable::get_table_id($parent_id);

$pagination = FeedbackTable::build_pagination($parent_id, $show_private, $count, $limit, $offset);

$pagination_html = $pagination['html'];
$pagination_js = $pagination['javascript'];

$inline_javascript = "addLoadEvent(function (){paginator = {$pagination_js}});";

$headers = PluginArtefactFeedback::get_headers();
$javascripts = array('paginator', 'artefact/feedback/js/feedback.js');

$form = add_parentfeedback_form::create($parent_id);

$block = new BlockInstance($parent_id);
$title = get_string('feedbacksfor', 'artefact.feedback', $block->get('title'));

$back_href = $view_id ? get_config('wwwroot') . '/view/view.php?id=' . $view_id : '';

$description = '';

$smarty = smarty($javascripts, $headers);
$smarty->assign('INLINEJAVASCRIPT', $inline_javascript);
$smarty->assign('PAGEHEADING', $title);
$smarty->assign('description', $description);
$smarty->assign('back_href', $back_href);
$smarty->assign_by_ref('data', $data);
$smarty->assign('count', $count);
$smarty->assign('parent_id', $parent_id);
$smarty->assign('show_form', $show_form);
$smarty->assign('view_id', $view_id);
$smarty->assign('can_edit', $can_edit);
$smarty->assign('form', $form);
$smarty->assign('table_id', $table_id);
$smarty->assign('pagination_html', $pagination_html);
$smarty->display('artefact:feedback:feedback_list.tpl');

/**
 *
 * @return string
 */
function get_request_url() {
    $r = array();
    foreach ($_REQUEST as $key => $val) {
        $r[] = $key . '=' . $val;
    }
    $r = implode('&amp;', $r);
    $result = $_SERVER['PHP_SELF'] . '?' . $r;
    return $result;
}

?>